﻿using System;
using System.Web;

namespace GroupChallenge.Web
{
    public class Global : System.Web.HttpApplication
    {
        public static GameManager GameMgr { get; set; }

        // Application starts - create a new game.
        protected void Application_Start(object sender, EventArgs e)
        {
            // CreateGameManager();     // Not working in IIS Integrated Mode
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            if (app != null)
            {
                HttpContext context = app.Context;

                // Attempt to perform first request initialization.
                GameManagerInitialization.Initialize(context);
            }
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e) { }
        protected void Application_Error(object sender, EventArgs e) { }
        protected void Session_Start(object sender, EventArgs e) { }
        protected void Session_End(object sender, EventArgs e) { }
        protected void Application_End(object sender, EventArgs e) { }

        internal class GameManagerInitialization
        {
            // IIS 7 - Integrated Mode - Initialization instead of creating from Application_Start.
            // Nice solution: http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx
            private static bool _Initialized = false;
            private static object _Lock = new object();

            // Initialize only on the first request.
            public static void Initialize(HttpContext context)
            {
                if (context == null ||
                    _Initialized)
                    return;

                lock (_Lock)
                {
                    if (_Initialized)
                        return;

                    // Initialize the GameManager once.
                    CreateGameManager(context);

                    _Initialized = true;
                }
            }

            // Create a new game.
            private static void CreateGameManager(HttpContext context)
            {
                // Current URI for client applications to reach GroupChallenge website resources.
                string resourcePath = "http://" + context.Request.Url.Authority + HttpRuntime.AppDomainAppVirtualPath;

                // Set the data path for data such as questions and images.
                GameMgr = new GameManager();

                // Set up the data paths and HTTP uri's.
                GameMgr.DataPath = context.Server.MapPath("~/App_Data");
                GameMgr.PlayerImagePath = context.Server.MapPath("~/PlayerImages");

                GameMgr.VirtualRootUrl = resourcePath;
                GameMgr.PlayerImagePathUrl = GameMgr.VirtualRootUrl + "/PlayerImages";
                GameMgr.ResetGame();

                context.Application["CurrentGameManager"] = GameMgr;
            }
        }
    }
}